<template>
  <div class="product-detail-container">
    <div class="page-header">
      <el-breadcrumb separator="/">
        <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
        <el-breadcrumb-item :to="{ path: '/products' }">产品中心</el-breadcrumb-item>
        <el-breadcrumb-item>{{ product?.name || '产品详情' }}</el-breadcrumb-item>
      </el-breadcrumb>
    </div>
    
    <div v-loading="loading" class="product-content">
      <template v-if="product">
        <div class="product-main">
          <div class="product-gallery">
            <el-image
              :src="product.image"
              fit="contain"
              :preview-src-list="product.images || [product.image]"
            />
            
            <div v-if="product.images && product.images.length > 1" class="product-thumbnails">
              <div
                v-for="(img, index) in product.images"
                :key="index"
                class="thumbnail"
                :class="{ active: currentImage === index }"
                @click="currentImage = index"
              >
                <el-image :src="img" fit="cover" />
              </div>
            </div>
          </div>
          
          <div class="product-info">
            <h1 class="product-title">{{ product.name }}</h1>
            <div class="product-category">
              <span class="label">分类：</span>
              <el-tag size="small">{{ product.categoryName || '未分类' }}</el-tag>
            </div>
            
            <div class="product-description">
              <h3>产品描述</h3>
              <p>{{ product.description }}</p>
            </div>
            
            <div class="product-attributes" v-if="product.attributes && product.attributes.length">
              <h3>产品参数</h3>
              <el-descriptions :column="1" border>
                <el-descriptions-item 
                  v-for="attr in product.attributes" 
                  :key="attr.key" 
                  :label="attr.key"
                >
                  {{ attr.value }}
                </el-descriptions-item>
              </el-descriptions>
            </div>
            
            <div class="product-actions">
              <el-button type="primary" size="large" @click="handleContact">
                <el-icon><Message /></el-icon>
                咨询产品
              </el-button>
              <el-button size="large" @click="goBack">
                <el-icon><Back /></el-icon>
                返回列表
              </el-button>
            </div>
          </div>
        </div>
        
        <div v-if="product.detail" class="product-detail-content">
          <h2>详细信息</h2>
          <div v-html="product.detail"></div>
        </div>
        
        <div class="related-products" v-if="relatedProducts.length > 0">
          <h2>相关产品</h2>
          <div class="related-grid">
            <div
              v-for="item in relatedProducts"
              :key="item.id"
              class="product-card"
              @click="goToProductDetail(item.id)"
            >
              <div class="product-image">
                <el-image :src="item.image" fit="cover" />
              </div>
              <div class="product-info">
                <h3 class="product-title">{{ item.name }}</h3>
                <p class="product-desc">{{ item.description }}</p>
              </div>
            </div>
          </div>
        </div>
      </template>
      
      <el-empty v-else-if="!loading" description="产品不存在或已下架" />
    </div>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import { useRoute, useRouter } from 'nuxt/app'
import { ElMessage } from 'element-plus'
import { Message, Back } from '@element-plus/icons-vue'

const route = useRoute()
const router = useRouter()
const productId = route.params.id

const product = ref(null)
const loading = ref(true)
const currentImage = ref(0)
const relatedProducts = ref([])

// 引入接口
import { getProductDetail } from '~/api/product'

// 获取产品详情
const fetchProductDetail = async () => {
  loading.value = true
  try {
    const res = await getProductDetail({ id: productId })
    console.log(res);
      product.value = {
        ...res,
        // 处理图片数组
        images: res.images ? JSON.parse(res.images) : [res.data.image],
        // 处理详情内容
        detail: res.details || ''
      }
  } catch (error) {
    console.error('获取产品详情出错:', error)
    ElMessage.error('获取产品详情失败')
  } finally {
    loading.value = false
  }
}

// 移除模拟数据相关代码
// const fetchRelatedProducts = async () => {
//   // 暂时移除相关产品逻辑
// }

onMounted(() => {
  fetchProductDetail()
  // fetchRelatedProducts() // 暂时注释掉相关产品的获取
})

// 返回列表
const goBack = () => {
  router.push('/products')
}

// 联系咨询
const handleContact = () => {
  ElMessage.success('已发送咨询请求，我们会尽快联系您')
  // 这里可以添加您的联系逻辑，如打开联系表单或跳转到联系页面
}
</script>

<style lang="scss" scoped>
.product-detail-container {
  max-width: 1200px;
  margin: 0 auto;
  padding: 20px;
}

.page-header {
  margin-bottom: 30px;
}

.product-content {
  min-height: 500px;
}

.product-main {
  display: flex;
  gap: 40px;
  margin-bottom: 40px;
}

.product-gallery {
  width: 45%;
  
  .el-image {
    width: 100%;
    height: 400px;
    border-radius: 8px;
    border: 1px solid #eee;
    background-color: #fff;
  }
}

.product-thumbnails {
  display: flex;
  gap: 10px;
  margin-top: 15px;
  overflow-x: auto;
  
  .thumbnail {
    width: 80px;
    height: 80px;
    border-radius: 4px;
    overflow: hidden;
    cursor: pointer;
    border: 2px solid transparent;
    
    &.active {
      border-color: var(--el-color-primary);
    }
    
    .el-image {
      width: 100%;
      height: 100%;
    }
  }
}

.product-info {
  flex: 1;
}

.product-title {
  font-size: 28px;
  margin: 0 0 20px;
  color: #333;
}

.product-category {
  margin-bottom: 20px;
  
  .label {
    color: #666;
    margin-right: 10px;
  }
}

.product-description {
  margin-bottom: 30px;
  
  h3 {
    font-size: 18px;
    margin-bottom: 10px;
    color: #333;
    position: relative;
    padding-left: 12px;
    
    &::before {
      content: '';
      position: absolute;
      left: 0;
      top: 50%;
      transform: translateY(-50%);
      width: 3px;
      height: 16px;
      background-color: var(--el-color-primary);
      border-radius: 1.5px;
    }
  }
  
  p {
    color: #666;
    line-height: 1.6;
  }
}

.product-attributes {
  margin-bottom: 30px;
  
  h3 {
    font-size: 18px;
    margin-bottom: 10px;
    color: #333;
    position: relative;
    padding-left: 12px;
    
    &::before {
      content: '';
      position: absolute;
      left: 0;
      top: 50%;
      transform: translateY(-50%);
      width: 3px;
      height: 16px;
      background-color: var(--el-color-primary);
      border-radius: 1.5px;
    }
  }
}

.product-actions {
  display: flex;
  gap: 15px;
  margin-top: 30px;
}

.product-detail-content {
  margin-top: 50px;
  padding: 30px;
  background: #fff;
  border-radius: 8px;
  box-shadow: 0 2px 12px rgba(0, 0, 0, 0.05);
  
  h2 {
    font-size: 22px;
    margin-bottom: 20px;
    color: #333;
    position: relative;
    padding-left: 15px;
    
    &::before {
      content: '';
      position: absolute;
      left: 0;
      top: 50%;
      transform: translateY(-50%);
      width: 4px;
      height: 20px;
      background-color: var(--el-color-primary);
      border-radius: 2px;
    }
  }
  
  h3 {
    font-size: 18px;
    margin: 25px 0 15px;
    color: #333;
  }
  
  p {
    color: #666;
    line-height: 1.6;
    margin-bottom: 10px;
  }
  
  ul {
    padding-left: 20px;
    margin-bottom: 15px;
    
    li {
      color: #666;
      line-height: 1.8;
    }
  }
}

.related-products {
  margin-top: 50px;
  
  h2 {
    font-size: 22px;
    margin-bottom: 20px;
    color: #333;
    position: relative;
    padding-left: 15px;
    
    &::before {
      content: '';
      position: absolute;
      left: 0;
      top: 50%;
      transform: translateY(-50%);
      width: 4px;
      height: 20px;
      background-color: var(--el-color-primary);
      border-radius: 2px;
    }
  }
}

.related-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(270px, 1fr));
  gap: 20px;
}

.product-card {
  background: #fff;
  border-radius: 8px;
  overflow: hidden;
  box-shadow: 0 2px 12px rgba(0, 0, 0, 0.05);
  transition: transform 0.3s, box-shadow 0.3s;
  cursor: pointer;

  &:hover {
    transform: translateY(-5px);
    box-shadow: 0 5px 15px rgba(0, 0, 0, 0.1);
  }
  
  .product-image {
    height: 180px;
    overflow: hidden;
    
    .el-image {
      width: 100%;
      height: 100%;
    }
  }
  
  .product-info {
    padding: 15px;
  }
  
  .product-title {
    font-size: 16px;
    margin: 0 0 10px;
    color: #333;
    font-weight: 600;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
  }
  
  .product-desc {
    font-size: 14px;
    color: #666;
    margin: 0;
    display: -webkit-box;
    -webkit-line-clamp: 2;
    -webkit-box-orient: vertical;
    overflow: hidden;
    height: 40px;
  }
}

@media (max-width: 768px) {
  .product-main {
    flex-direction: column;
  }
  
  .product-gallery {
    width: 100%;
    margin-bottom: 30px;
  }
  
  .product-actions {
    flex-direction: column;
  }
}
</style>